package cn.example.common.protocol.modules;

import org.artifact.core.lang.IServer;
import org.artifact.core.context.session.ISession;
import org.artifact.core.context.packet.IPacket;
import cn.hutool.core.map.MapProxy;
import java.util.function.Consumer;
import java.util.Collection;
import java.util.Map;
import java.util.List;
import org.artifact.core.constant.ArtifactErrorCode;
import org.artifact.core.exception.ArtifactExceptionUtil;
import org.artifact.core.lang.IModule;
import cn.example.common.protocol.structs.*;

@SuppressWarnings("unchecked")
public interface StoreModule extends IModule{
	
	default int getModuleId(){
		return 30945;
	}
	
	/** 分发  */
	default Consumer<IPacket> disp(ISession session,IPacket packet) {
		switch (packet.getMethodId()) {
			case 19568:{
				return buyGoods(session,packet);
			}
			default:
				throw ArtifactExceptionUtil.error(ArtifactErrorCode.NOT_FOND_ACTION);
		}
	}	


	/** 购买商品 */
	default Consumer<IPacket> buyGoods(ISession session,IPacket packet) {
		MapProxy proxy = MapProxy.create(packet.getBody());
		long id = proxy.getLong(3355);
		int num = proxy.getInt(109446);
		int storeId = proxy.getInt(-1884271140);
		return buyGoods(session, id, num, storeId);
	}
	
	/** 购买商品 */
	Consumer<IPacket> buyGoods(ISession session, long id, int num, int storeId);
	
	default Consumer<IPacket> _buyGoods(ISession session){
		return (IPacket packet) -> {
                if (packet == null){
                    packet = IServer.me().getContext().getPacketFactory().create();
                    packet.setCmd(30945 << 16 ^ 19568);
                } else {
                    packet.getBody().clear();
                }

				session.sendPacket(packet);
			};
	}
	
	
	/** 刷新商品 */
	default void refreshGoods(ISession session, long goodsId, int storeId){
		IPacket packet = IServer.me().getContext().getPacketFactory().create();
		packet.setCmd(30945 << 16 ^ 9531);
		packet.getBody().put(207037201,goodsId);
		packet.getBody().put(-1884271140,storeId);
		session.sendPacket(packet);
	}
	
	default void refreshGoods(Collection<ISession> sessions, long goodsId, int storeId){
		IPacket packet = IServer.me().getContext().getPacketFactory().create();
		packet.setCmd(30945 << 16 ^ 9531);
		packet.getBody().put(207037201,goodsId);
		packet.getBody().put(-1884271140,storeId);
		for (ISession session : sessions) {
			session.sendPacket(packet);
		}
	}
	
}